#!/usr/bin/env python
# coding=utf-8

from doubly_linked_base import DoublyLinkedBase
from execeptions.empty import Empty

class LinkedDeque(DoublyLinkedBase):
    """通过继承DoublyLinkedBase类实现用双端链表存储方式的双端队列"""

    def first(self):
        """返回双端队列的第一个结点"""
        if self.is_empty():
            raise Empty("Deque is empty!")
        return self._header._next._element

    def last(self):
        """返回双端队列的最后一个结点"""
        if self.is_empty():
            raise Empty("Deque is empty!")
        return self._tailer._prev._element

    def insert_first(self, e) -> _Node:
        """在双端队列的队头插入结点"""
        return self._insert_between(e, self._header, self._header._next)

    def insert_last(self, e) -> _node:
        """在双端队列的队尾插入结点"""
        return self._insert_between(e, self._tailer._prev, self._tailer)

    def delete_first(self):
        """在双端队列的队头删除结点"""
        if self.is_empty():
            raise Empty("Deque is empty!")
        return self._delete_node(self._header._next)

    def delete_last(self):
        """在双端队列的队尾删除结点"""
        if self.is_empty():
            raise Empty("Deque is empty!")
        return self._delete_node(self._tailer._prev)

